package springBoot.akai.Akai.Mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import springBoot.akai.Akai.Entity.Tag;
import springBoot.akai.Akai.Entity.Video;

import java.util.List;

@Mapper
@Repository
public interface TagMapper {
	@Select("SELECT last_insert_rowid()")
	int selectLastInsertId();
	
	@Select("SELECT * FROM Tag WHERE id = #{tagId}")
	Tag selectById(int tagId);
	
	@Select("SELECT id FROM Tag WHERE name = #{tagName}")
	Integer selectTagIdByName(String tagName);
	
	@Select("SELECT t.* FROM Tag AS t, Video_Tag as vt WHERE vt.video_id = #{videoId} AND t.id = vt.tag_id")
	List<Tag> selectByVideoId(int videoId);
	
	@Insert("INSERT INTO Tag(name) VALUES(#{tagName})")
	boolean insertTag(String tagName);
	
	@Insert("INSERT INTO Video_Tag(tag_id, video_id) VALUES(#{tagId}, #{videoId})")
	boolean createTagVideoLink(int tagId, int videoId);
	
	@Delete("DELETE FROM Tag WHERE id = #{tagId}")
	boolean deleteTagById(int tagId);
	
	@Delete("DELETE FROM Video_Tag WHERE tag_id = #{tagId} AND video_id = #{videoId}")
	boolean deleteTagVideoLink(int tagId, int videoId);
	
	@Select("SELECT v.* FROM Video as v, Video_Tag as vt WHERE vt.tag_id = #{tagId} AND v.id = vt.video_id")
	List<Video> selectVideoByTagId(int tagId);
}
